package com.itouch.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileTools {

	public static void stringWrite2File(String fileAbPath, String data) {
		writeByteToFile(data.getBytes(), fileAbPath);
	}

	public static void delete(File file) {
		if (file.isFile()) {
			file.delete();
			return;
		}

		if (file.isDirectory()) {
			File[] childFiles = file.listFiles();
			if (childFiles == null || childFiles.length == 0) {
				file.delete();
				return;
			}

			for (int i = 0; i < childFiles.length; i++) {
				delete(childFiles[i]);
			}
			file.delete();
		}
	}

	public static String getFileName(String currentFileName, int num) {
		File file = new File(currentFileName);
		if (num == 0) {
			if (!file.exists()) {
				return currentFileName;
			} else {
				return getFileName(currentFileName, (num + 1));
			}
		} else {
			String fileFullName = file.getName();
			String parent = file.getParent();
			String fileType = fileFullName.substring(fileFullName
					.lastIndexOf(".") + 1);
			String fileName = fileFullName.substring(0,
					fileFullName.lastIndexOf("."));

			String newFileName = parent + "/" + fileName + num + "." + fileType;
			File newFile = new File(newFileName);
			if (!newFile.exists()) {
				return newFileName;
			} else {
				return getFileName(currentFileName, (num + 1));
			}
		}
	}

	public static String writeByteToFile(byte[] fileByte, String abPath) {
		File file = new File(abPath);
		return writeByteToFile(fileByte, file.getParent(), file.getName());
	}

	public static String writeByteToFile(byte[] fileByte, String filePath,
			String fileFullName) {
		File file = new File(filePath);
		String myFilePath = filePath + "/" + fileFullName;
		if (!file.exists()) {
			file.mkdirs();
		}

		File fileAbPath = new File(myFilePath);
		if (fileAbPath.exists()) {
			fileAbPath.delete();

		}

		try {
			fileAbPath.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		try {
			FileOutputStream out = new FileOutputStream(myFilePath);
			out.write(fileByte);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return myFilePath;
	}

	public static byte[] getFileToByte(String fileString) throws IOException {
		File inFile = new File(fileString);

		FileInputStream fileInputStream = new FileInputStream(inFile);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int count = 0;
		while ((count = fileInputStream.read(buffer)) > 0) {
			byteArrayOutputStream.write(buffer, 0, count);
		}
		fileInputStream.close();
		// 把文件存在一个字节数组中
		return byteArrayOutputStream.toByteArray();

	}

	public static long getFileLength(String filePath) {
		File f = new File(filePath);
		Long len = f.length();
		return len;
	}

	public static String htmlData(String data) {
		StringBuffer strbuffer = new StringBuffer();
		strbuffer.append("<html>\n");
		strbuffer.append("<head> \n");
		strbuffer.append("<style type=\"text/css\"> \n");
		strbuffer.append("body {font-size: 14.0; font-family: \"宋体\";}\n");
		strbuffer.append("p {color:#6C6C6C;}");
		strbuffer.append("</style> \n");
		strbuffer.append("</head> \n");
		strbuffer.append("<body> \n");
		strbuffer.append("<p>");
		strbuffer.append(data);
		strbuffer.append("</p>");
		strbuffer.append("</body> \n");
		strbuffer.append("</html>");
		return strbuffer.toString();
	}

	public static String getSdcardDir() {
//		File sdDir = null;
//		boolean sdCardExist = Environment.getExternalStorageState().equals(
//				Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
//		Environment.
//		if (sdCardExist) {
//			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
//		}
		return "/mnt/sdcard/";
	}
}
